//
//  CXHSplashViewController.m
//  CXHAdSDK-Code_Example
//
//  Created by 陈坤 on 2022/9/28.
//  Copyright © 2022 华资融创. All rights reserved.
//

#import "CXHSplashViewController.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
#import "CXHAdSDK-Core/CXHAdSDKSplashAd.h"
#if __has_include(<CXHAdSDK-Kit/CXHUtilsKit.h>)
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#else
#import "CXHUtilsKit.h"
#endif

@interface CXHSplashViewController ()<CXHAdSDKSplashAdDelegate>

@property (nonatomic, strong) CXHAdSDKSplashAd *splashAd;
@property (nonatomic) UIActivityIndicatorView *loadingView;

@end

@implementation CXHSplashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    // 在模态视图控制器中添加一个 loading 控件
    self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.loadingView.center = self.view.center;
    self.loadingView.color = [UIColor blackColor];
    [self.view addSubview:self.loadingView];
    [self.loadingView startAnimating];
}

- (void)viewDidAppear:(BOOL)animated{
    if (self.splashAd) {
        return;
    }
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            if ([NSThread isMainThread]) {
                [self loadSplashAd ];
            } else {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    // 非主线程，将UI操作切换到主线程进行，请求广告
                    [self loadSplashAd ];
                });
            }
        }];
    } else {
        [self loadSplashAd ];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"CXHSplashAd->touchesBegan...");
    if (self.splashAd) {
        return;
    }
    [self dismiss];
}

- (void)loadSplashAd{
    if (self.splashAd) {
        return;
    }

    // 其他相关的配置请参考文档
    // 1、初始化开屏广告
    self.splashAd = [[CXHAdSDKSplashAd alloc]init];
    self.splashAd.delegate = self;
    self.splashAd.showTime = 5;
    self.splashAd.controller = self;
    // 2、设置posId
    /// TODO 替换成自己的广告位
    self.splashAd.posId = @"09785892";
    self.splashAd.setSkipButtonToRightTop = YES;
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.1);
    self.splashAd.bottomView = bottomView;
    [self.splashAd loadSplashAd];
}

- (void)dismiss{
    self.splashAd = nil;
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)onCancelClicked:(id)sender {
    NSLog(@"CXHSplashAd->onCancelClicked...");
}

#pragma mark - CXHAdSDKSplashAdDelegate
/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    NSLog(@"CXHSplashAd->CXH_splashAdDidClick...");
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"CXHSplashAd->CXH_splashAdDidClose...");
    [self dismiss];
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"CXHSplashAd->CXH_splashAdExposured...");
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"CXHSplashAd->CXH_splashAdDidLoad...");
    if (self.loadingView) {
        [self.loadingView stopAnimating];
        [self.loadingView removeFromSuperview];
        self.loadingView = nil;
    }
    
    if ([self.splashAd isAdValid]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        [self.splashAd showSplashAdInWindow:window];
    } else {
        [self dismiss];
    }
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    NSLog(@"CXHSplashAd->CXH_splashAdDidFailToLoad : %@", error.localizedDescription);
    if (self.loadingView) {
        [self.loadingView stopAnimating];
        [self.loadingView removeFromSuperview];
        self.loadingView = nil;
    }
    [self dismiss];
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"CXHSplashAd->CXH_splashAdSuccessPresentScreen...");
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    NSLog(@"CXHSplashAd->CXH_splashAdFailToPresent : %@", error.localizedDescription);
    [self dismiss];
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
    NSLog(@"CXHSplashAd->CXH_splashAdCloseLandingPage...");
    [self dismiss];
}


@end
